package com.hzdesk.media.koreabatchmatvector.contral;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;

import com.hzdesk.media.koreabatchmatvector.inter.ISketchpadDraw;


/*function:
 * @author:
 * Date:
 */
public class PlygonCtl implements ISketchpadDraw {
	private Path mPath = new Path();
	private Paint mPaint = new Paint();
	private boolean m_hasDrawn = false;
	private static vertexDefine mPoint = new vertexDefine();
	private static vertexDefine startPoint = new vertexDefine();
	private static vertexStack pointStack = new vertexStack();
	private static int countLine = 0;

	public PlygonCtl(int penSize, String penColor) {
		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setColor(Color.parseColor("#"+penColor));
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(penSize);
	}

	public void cleanAll() {
		mPath.reset();
	}

	public void draw(Canvas canvas) {
		if (null != canvas) {
			canvas.drawPath(mPath, mPaint);
			Log.i("sada022", "plygon02");
		}
	}

	public boolean hasDraw() {
		return m_hasDrawn;
	}

	public void touchDown(float x, float y) {
		mPath.reset();
		if (pointStack.isEmpty()) {
			mPoint.setPoint(x, y);
			startPoint.setPoint(mPoint.getX(), mPoint.getY());
		} else {
			mPoint = pointStack.pop();
			if (mPoint.getX() >= startPoint.getX() - 1 && mPoint.getX() <= startPoint.getX() + 1
					&& mPoint.getY() >= startPoint.getY() - 1 && mPoint.getY() <= startPoint.getY() + 1) {
				touchDown(x, y);  
				Log.i("start", "startPointX" + startPoint.getX() + "," + "startPointY" + startPoint.getY());
				Log.i("end", "mPointX" + mPoint.getX() + "," + "mPointY" + mPoint.getY());
				return;
			}
		}
		mPath.moveTo(mPoint.getX(), mPoint.getY());
	}

	public void touchMove(float x, float y) {
		mPoint.setPoint(x, y);
		m_hasDrawn = true;
	}

	public void touchUp(float x, float y) {
		mPoint.setPoint(x, y);
		pointStack.push(mPoint);
		mPath.lineTo(mPoint.getX(), mPoint.getY());
		PlygonCtl.countLine++;
	}

	public void lineDraw(Canvas canvas) {
		canvas.drawLine(mPoint.getX(), mPoint.getY(), startPoint.getX(), startPoint.getY(), mPaint);
		pointStack.pop();
	}

	public static vertexDefine getmPoint() {
		return mPoint;
	}

	public static vertexDefine getStartPoint() {
		return startPoint;
	}

	public static void setmPoint(float pointX, float pointY) {
		PlygonCtl.mPoint.setX(pointX);
		PlygonCtl.mPoint.setY(pointY);
	}

	public static int getCountLine() {
		return countLine;
	}

	public static void setCountLine(int countLine) {
		PlygonCtl.countLine = countLine;
	}

}
